package com.guinea.core.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.acegisecurity.context.SecurityContextHolder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.guinea.core.acegi.cache.UserDetailsImpl;

public class CommonUtils {

	protected static final Log logger = LogFactory.getLog(CommonUtils.class);
	
	public static Long getCurrentUserId(){
		Long userId = null;
		Object obj =SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if (obj instanceof UserDetailsImpl) {
		    userId =((UserDetailsImpl)obj).getId();
		}
		return userId;
	}
	/**
	 * 输入输出通用方法
	 * 
	 * @auther huaxiaz
	 * @since 2009-7-27
	 * @param in
	 * @param out
	 * @return
	 */
	public static boolean io(InputStream in, OutputStream out) {
		boolean b = true;
		byte[] buffer = new byte[1024 * 4];
		try {
			int readLength;
			while ((readLength = in.read(buffer)) > -1) {
				out.write(buffer, 0, readLength);
			}
			out.flush();
			out.close();
			in.close();
		} catch (IOException e) {
			b = false;
			logger.error("io IOException errorMessage :" + e.getMessage());
		} finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e1) {
				logger.error("io finally IOException  errorMessage :"
						+ e1.getMessage());
			}
		}
		return b;
	}
}
